#include<iostream>
#include<sstream>
#include<string>
#include<streambuf>
#include<fstream>
#include<queue>

using namespace std;


class IStreamQueue : std::istream
{
    using QUEUE = queue<istream*>;
    QUEUE q;
public:
    IStreamQueue() : q()          {     }

    void push(istream* is_ptr) {
        q.push(is_ptr);
    }

    istream& get(char& c) {
        if(q.empty()) {
            c = EOF;
            setstate(ios_base::eofbit);
            return *this;
        } else if (q.front()->get(c)){
            return *q.front();
        } else {
            // delete q.front();
            q.pop();
            return get(c); 
        }
    }
};


int main(int args, char** argv)
{   
    IStreamQueue isq;

    stringstream ss1("ss1\n");
    stringstream ss2("ss2\n");

    for(int i = 1; i < args; i++) {
        isq.push(new ifstream(argv[i]));
    }

    isq.push(&ss1);
    isq.push(&ss2);

    isq.push(&cin);

    char c;
    while (isq.get(c))
    {
        cout<<c;
    }
    
    cout << "ALL END" << endl;
    return 0;
}

